package com.yq.cp.controller.basics;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.cp.entity.basics.Area;
import com.yq.cp.entity.basics.Areas;
import com.yq.cp.entity.basics.Cities;
import com.yq.cp.entity.basics.Provinces;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.service.basics.AreaService;
import com.yq.cp.service.basics.AreasService;
import com.yq.cp.service.basics.CitiesService;
import com.yq.cp.service.basics.ProvincesService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 大区-省市区 前端控制器
 * </p>
 *
 * @author shenyuqing
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/area")
public class AreaController {
    @Resource
    AreaService areaService;
    @Resource
    CitiesService citiesService;
    @Resource
    AreasService areasService;
    @Resource
    ProvincesService provincesService;

    /**
     * 添加编辑
     * @param area
     * @return
     */
    @PostMapping("add")
    public ApiResult add(@RequestBody Area area){
        areaService.saveOrUpdate(area);
        return ApiResult.success();
    }

    /**
     * 大区查询
     * @param vo
     * @return
     */
    @PostMapping("list1")
    public ApiResult list1(@RequestBody PageVo vo){
        IPage<Area> page = new Page<>(vo.getPage(), vo.getSize());
        QueryWrapper<Area> wrapper = new QueryWrapper<>();
        if (!Objects.isNull(vo.getLike())&&!"".equals(vo.getLike()))
            wrapper.like("name",vo.getLike());
        wrapper.eq("parent_id",0);
        page = areaService.page(page,wrapper);
        if (page.getRecords().size()>0){
            for (Area record : page.getRecords()) {
                record.setDaqu(record.getName());
                record.setDaquId(record.getId());
            }
        }
        return ApiResult.success(page);
    }
    /**
     * 省查询
     * @param vo
     * @return
     */
    @PostMapping("list2")
    public ApiResult list2(@RequestBody PageVo vo){
        IPage<Area> page = new Page<>(vo.getPage(), vo.getSize());
        QueryWrapper<Area> wrapper = new QueryWrapper<>();
        if (!Objects.isNull(vo.getLike())&&!"".equals(vo.getLike()))
            wrapper.like("name",vo.getLike());
        wrapper.eq("parent_id",vo.getId());
        page = areaService.page(page,wrapper);
        if (page.getRecords().size()>0){
            for (Area record : page.getRecords()) {
                Area byId = areaService.getById(record.getParentId());
                record.setDaqu(byId.getName());
                record.setDaquId(vo.getId());
                Provinces byId1 =provincesService.getOne(new QueryWrapper<Provinces>()
                        .eq("provinceid",record.getShengId()));
                record.setSheng(byId1.getProvince());
            }
        }
        return ApiResult.success(page);
    }

    /**
     * 市查询
     * @param vo
     * @return
     */
    @PostMapping("list3")
    public ApiResult list3(@RequestBody PageVo vo){
        Page<Cities> citiesPage = new Page<>(vo.getPage(), vo.getSize());
        Area byId1 = areaService.getById(vo.getId());
        QueryWrapper<Cities> wrapper = new QueryWrapper<>();
        if (!Objects.isNull(vo.getLike())&&!"".equals(vo.getLike()))
            wrapper.like("city",vo.getLike());
        wrapper.eq("provinceid",vo.getParentId());
        Provinces byId =provincesService.getOne(new QueryWrapper<Provinces>()
                .eq("provinceid",vo.getParentId()));
        //Provinces byId = provincesService.getById(vo.getParentId());
        citiesPage = citiesService.page(citiesPage,wrapper);
        for (Cities record : citiesPage.getRecords()) {
            record.setDaqu(byId1.getName());
            record.setDaquId(vo.getId());
            record.setSheng(byId.getProvince());
            record.setShi(record.getCity());
            record.setShiId(Integer.parseInt(record.getCityid()));
        }
        return ApiResult.success(citiesPage);
    }

    /**
     * 区查询
     * @param vo
     * @return
     */
    @PostMapping("list4")
    public ApiResult list4(@RequestBody PageVo vo){
        Page<Areas> areasPage = new Page<>(vo.getPage(), vo.getSize());
        QueryWrapper<Areas> wrapper = new QueryWrapper<>();
        if (!Objects.isNull(vo.getLike())&&!"".equals(vo.getLike()))
            wrapper.like("area",vo.getLike());
        wrapper.eq("cityid",vo.getParentId());
        areasPage = areasService.page(areasPage,wrapper);
        Cities byId =citiesService.getOne(new QueryWrapper<Cities>()
                .eq("cityid",vo.getParentId()));
        //Cities byId = citiesService.getById(vo.getParentId());
        Provinces byId1 =provincesService.getOne(new QueryWrapper<Provinces>()
                .eq("provinceid",byId.getProvinceid()));
        //Provinces byId1 = provincesService.getById(byId.getProvinceid());
        Area byId2 = areaService.getById(vo.getId());
        for (Areas record : areasPage.getRecords()) {
            record.setQu(record.getArea());
            record.setShi(byId.getCity());
            record.setSheng(byId1.getProvince());
            record.setDaqu(byId2.getName());
            record.setQuId(Integer.parseInt(record.getAreaid()));
        }

        return ApiResult.success(areasPage);
    }

    /**
     * 删除
     * @param area
     * @return
     */
    @PostMapping("remove")
    public ApiResult remove(@RequestBody Area area){
        areaService.removeById(area);
        return ApiResult.success();
    }
}

